/*
 * VHRRMatchGrid.java
 * Created on 13 juil. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.res.VHResConstants;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.ui.menu.VHGridContextMenu;
import com.hockeo.vhweb.client.ui.menu.VHMatchInfoContextMenu;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Renders a grid that displays round-robin matches and results.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHRRMatchGrid extends LayoutContainer implements VHRpcResultReceiver<List<VHJsData>>
{
  public static final int               HEIGHT_MATCHDAY_HEADER = 29;
  public static final int               HEIGHT_MATCH           = 24;

  private final VHGridContextMenu   d_ctxmenu;
  private final GroupingStore<VHJsData> d_store;
  private Grid<VHJsData>                d_grid;

  /**
   * Builds a new RR match grid.
   */
  public VHRRMatchGrid( VHController controller )
  {
    d_store = new GroupingStore<VHJsData>();

    buildGrid();
    setLayout( new FitLayout() );
    add( d_grid );

    d_ctxmenu = new VHMatchInfoContextMenu( controller, d_grid, "clubHome_", "clubAway_" );
  }

  public void setRpcData( List<VHJsData> listMatches, String strPurpose )
  {
    d_store.removeAll();
    d_store.add( listMatches );
    d_store.groupBy( "matchday_num" );
  }

  private void buildGrid()
  {
    final VHResConstants constants = VHMain.getConstants();

    // Cell rendering
    //
    GridCellRenderer<VHJsData> renderer = new GridCellRenderer<VHJsData>() {
      public String render( VHJsData m, String strProperty, ColumnData config, int rowIndex, int colIndex,
          ListStore<VHJsData> store, Grid<VHJsData> grid )
      {
        if ( strProperty.equals( "clubHome_country" ) )
          return "<img src='images/flags/16x16/" + m.getString( "clubHome_country" ) + ".png'/>";
        else if ( strProperty.equals( "clubAway_country" ) )
          return "<img src='images/flags/16x16/" + m.getString( "clubAway_country" ) + ".png'/>";
        else if ( strProperty.equals( "clubHome_name" ) && m.getString( "winner_id" ) != null
            && m.getString( "winner_id" ).equals( m.getString( "clubHome_id" ) ) )
          return "<span style='font-weight: bold;'>" + m.getString( "clubHome_name" ) + "</span>";
        else if ( strProperty.equals( "clubAway_name" ) && m.getString( "winner_id" ) != null
            && m.getString( "winner_id" ).equals( m.getString( "clubAway_id" ) ) )
          return "<span style='font-weight: bold;'>" + m.getString( "clubAway_name" ) + "</span>";
        else if ( strProperty.equals( "goalsHome" ) )
        {
          if ( !m.getBoolean( "played" ) )
            return constants.matchGoalsNotPlayed();

          String strResult = m.getInt( "goalsHome" ) + constants.matchGoalsSeparator() + m.getInt( "goalsAway" );

          if ( m.getBoolean( "decidedByShootout" ) )
            strResult += " <span title='" + constants.afterShootoutToolTip() + "'>" + constants.afterShootout()
                + "</span>";

          return strResult;
        }

        return String.valueOf( m.get( strProperty ) );
      }
    };

    // Column definitions
    //
    ArrayList<ColumnConfig> listCols = new ArrayList<ColumnConfig>();
    ColumnConfig col;

    col = new ColumnConfig();
    col.setId( "matchday_num" );
    col.setWidth( 26 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( false );
    col.setHidden( true );
    col.setMenuDisabled( true );
    col.setSortable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubHome_country" );
    col.setWidth( 26 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    col.setGroupable( false );
    col.setMenuDisabled( true );
    col.setSortable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubHome_name" );
    col.setWidth( 200 );
    col.setRenderer( renderer );
    col.setGroupable( false );
    col.setMenuDisabled( true );
    col.setSortable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubAway_country" );
    col.setWidth( 26 );
    col.setRenderer( renderer );
    col.setAlignment( HorizontalAlignment.CENTER );
    col.setResizable( false );
    col.setGroupable( false );
    col.setMenuDisabled( true );
    col.setSortable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "clubAway_name" );
    col.setWidth( 200 );
    col.setRenderer( renderer );
    col.setGroupable( false );
    col.setMenuDisabled( true );
    col.setSortable( false );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "goalsHome" );
    col.setWidth( 50 );
    col.setRenderer( renderer );
    col.setGroupable( false );
    col.setMenuDisabled( true );
    col.setSortable( false );
    listCols.add( col );

    final ColumnModel cm = new ColumnModel( listCols );

    // Header cells with tooltips
    //
    GroupingView view = new GroupingView();

    view.setForceFit( true );
    view.setGroupRenderer( new GridGroupRenderer() {
      public String render( GroupColumnData data )
      {
        VHJsData matchFirst = (VHJsData) data.models.get( 0 );

        if ( matchFirst.get( "matchday_moveday_date" ) != null )
          return VHMain.getMessages().matchdayHeaderWithDate( matchFirst.getInt( "matchday_moveday_id" ),
              matchFirst.getInt( "matchday_num" ), matchFirst.getDate( "matchday_moveday_date" ) );

        return VHMain.getMessages().matchdayHeader( matchFirst.getInt( "matchday_moveday_id" ),
            matchFirst.getInt( "matchday_num" ) );
      }
    } );

    // Grid
    //
    d_grid = new Grid<VHJsData>( d_store, cm );
    d_grid.setView( view );
    d_grid.setHideHeaders( true );
    d_grid.setStripeRows( true );
    d_grid.setBorders( true );
    d_grid.setAutoWidth( true );
  }
}
